#include <stdio.h>
#include <math.h>

#include "Auxiliary.h"
#include "na_1.h"

namespace na_12_ns
{

inline double itrFunction1( const double x0 )
{
    return x0 * x0 + x0 - 3.0;
}

inline double itrFunction2( const double x0 )
{
    return 3.0 / ( x0 * x0 );
}

inline double itrFunction3( const double x0 )
{
    return x0 - ( x0 * x0 - 3.0 ) / 8.0;
}

inline double itrFunction4( const double x0 )
{
    return x0 - ( x0 * x0 - 3.0 ) / 4.0;
}

inline double itrFunction5( const double x0 )
{
    return x0 - ( x0 * x0 - 3.0 );
}

inline double itrFunction6( const double x0 )
{
    double dbx0 = 2.0 * x0;
    double t = x0 - ( x0 * x0 - 3.0) / dbx0;
    return t - ( t * t - 3.0 )/ dbx0;
}

int na_12()
{
    printf("***************************************************\n");
    printf("Start the NA experiment 1.2, basic iteration (2)\n");

    const double    inputValue = 2.0; //the given init value
    char            funcNo = 0;
    itrFunctor      itrFunc[6]; 

    itrFunc[0] = &itrFunction1;
    itrFunc[1] = &itrFunction2;
    itrFunc[2] = &itrFunction3;
    itrFunc[3] = &itrFunction4;
    itrFunc[4] = &itrFunction5;
    itrFunc[5] = &itrFunction6;

    printf("Please input the iteration funcNo ( 1 ~ 6 ):");
    static const char cValidInput[] = { '1', '2', '3', '4', '5', '6' };
    static const unsigned int cVInputCnt = 6;
    funcNo = CheckedInputChar( cValidInput, cVInputCnt ) - '1';
    
    printf("Start iteration with equation %d, first from init value x0 = %1.2f \n", funcNo, inputValue);

    ItrProcess( itrFunc[funcNo], inputValue );

    printf("End of iteration\n");
    printf("***************************************************\n");

	return 0;
}

}
